const bannerModel = require('../../model/banner/bannerModel')
class bannerService{
  //获取轮播图
  async getBanneres(req,res){
    const {results:data} = await bannerModel.banner()
    if(data.length !=0){
      return res.json({
        data,
        meta:{
          msg:'查询文章成功',
          status: 200
        }
      })
    }else{
      return res.json({
        meta: {
          msg: '查询文章失败',
          status: 404
        }
      })
    }
  }
  //查询轮播图列表
  async getBanner(req,res){
    let { page, per_page } = req.query
    
    console.log('page = ' + page)
    per_page = parseInt(per_page)
    console.log('per_page = ' + per_page)
    if (page == undefined) {
      page = 1
    }
    if(per_page == undefined){
      per_page = 10
    }
    let pagenum = null
    if (per_page === 1) {
      pagenum = page - 1
    } else if (per_page === 2) {
      pagenum = (page - 1) * 2
    } else if (per_page === 5) {
      pagenum = (page - 1) * 5
    } else if (per_page === 10) {
      pagenum = (page - 1) * 10
    }
    const {results:data1} = await bannerModel.searchBanner(pagenum, per_page)
    const {results:data2} = await bannerModel.banner()
    let datas = {
      'pagenum':pagenum,
      'total':data2.length,
      'banners':data1,
    }
    if(datas.length !=0){
      return res.json({
        datas,
        meta:{
          msg:'查询文章成功',
          status: 200
        }
      })
    }else{
      return res.json({
        meta: {
          msg: '查询文章失败',
          status: 404
        }
      })
    }
  }

  //ID查询轮播图ID
  async getBannerID(req,res){
    const id = req.query.id
    console.log(req.query)
    const {results:data} = await bannerModel.queryBannerID(id)
    if(data.length != 0){
      return res.json({
        data:data[0],
        meta: {
          msg: '文章获取成功',
          status: 200
        } 
      })
    }else{
      return res.json({
        meta: {
          msg: '文章获取失败',
          status: 404
        } 
      })
    }
  }

  //修改轮播图
  async postUpdateBanner(req,res){
    let {title,img,EnglishName,describes,btnName,href,id} = req.body
    const {results:data} =await bannerModel.queryBannerID(id)
    if(data.length != 0){
      const update = await bannerModel.updateBanner(title,img,EnglishName,describes,btnName,href,id)
      res.json({
        data:data[0],
        meta:{
          msg: '轮播图修改成功',
          status: 200
        }
      })
    }else{
      res.json({
        meta:{
          msg: '轮播图修改失败',
          status: 404
        }
      })
    }
  }
  
  //新增轮播图
  async postAddBanner(req,res){
    const {title,img,EnglishName,describe,btnName,href} = req.body
    const {results:data} = await bannerModel.addBanner(title,img,EnglishName,describe,btnName,href)
    if(data.length != 0){
      return res.json({
        data,
        meta: {
          msg: '轮播图新增成功',
          status: 200
        } 
      })
    }else{
      return res.json({
        meta: {
          msg: '轮播图新增失败',
          status: 404
        }
      })
    }
  }

  //删除轮播图
  async postDleteBanner(req,res){
    let id = req.body.id
    const {results:data} = await bannerModel.queryBannerID(id)
    if(data.length !=0){
      const deleteID = await bannerModel.deleteBanner(id)
      return res.json({
        data:data[0],
        meta: {
          msg: '删除轮播图成功',
          status: 200
        } 
      })
    }else{
      return res.json({
        meta: {
          msg: '删除轮播图失败',
          status: 404
        }
      })
    }
  }
}
module.exports = new bannerService()